package com.pcbly.web.components.utils.converters;

import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.StringConverter;

import com.pcbly.web.components.utils.TimeOp;



public class MyStringConverter implements Converter {
	private StringConverter originalStrConverter;
	private static String newDefaultString()
    {
        return "";
    }
	@SuppressWarnings("unchecked")
    public Object convert(Class type, Object value)
    {
        if(type.equals(java.lang.String.class))
        {
            if(value == null)
                return newDefaultString();
            if(value.getClass().equals(java.util.Date.class))
                return TimeOp.convertDateTimeToString((Date)value, 2);
            if(value.getClass().equals(java.sql.Date.class))
                return TimeOp.convertDateTimeToString((java.sql.Date)value, 2);
            if(value.getClass().equals(java.sql.Timestamp.class))
                return TimeOp.convertDateTimeToString((Timestamp)value, 4);
            else
                return originalStrConverter.convert(type, value);
        } else
        {
            return originalStrConverter.convert(type, value);
        }
    }
    public MyStringConverter(){
    	originalStrConverter = new StringConverter();
    }

}
